package com.yuhuan.sowordtooffer;

import java.util.ArrayList;
import java.util.Arrays;
/**
 * 剑指offer06 从头到尾打印链表
 * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * 输入：head = [1,3,2]
 * 输出：[2,3,1]
 *
 **/
public class PrintLinklist {
    ArrayList<Integer> tmp = new ArrayList();
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        System.out.println(Arrays.toString(new PrintLinklist().convert(node1)));
        System.out.println(Arrays.toString(new PrintLinklist().reversePrint(node1)));

        ArrayList<Integer> a = new ArrayList();
        a.add(3);
        a.add(5);
    }
    void recur(ListNode head) {
        if (head == null) return;
        recur(head.next);
        tmp.add(head.node);
    }
    public int[] convert(ListNode listNode) {
        recur(listNode);
        int a[] = new int[tmp.size()];
        for (int i = 0; i < tmp.size(); i++) {
            a[i] = tmp.get(i);
        }
        return a;
    }

    public int[] reversePrint(ListNode head) {
        // 找到node长度
        int length = 0;
        ListNode tmp = head;
        while(tmp != null) {
            length++;
            tmp = tmp.next;
        }
        int res[] = new int [length];
        for(int i = 1; i<=length; i++){
            res[length - i] = head.node;
            head = head.next;
        }
        return  res;
    }

}
